#!/bin/bash

###
### This file is part of icecode-project.
###
###    icecode-project is free software: you can redistribute it and/or modify
###    it under the terms of the GNU General Public License as published by
###    the Free Software Foundation, either version 3 of the License, or
###    (at your option) any later version.
###
###    icecode-project is distributed in the hope that it will be useful,
###    but WITHOUT ANY WARRANTY; without even the implied warranty of
###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
###    GNU General Public License for more details.
###
###    You should have received a copy of the GNU General Public License
###    along with icecode-project.  If not, see <http://www.gnu.org/licenses/>.
###
###    Copyright 2012 Alfredo Mungo
###

###
### EXIT STATUS:
###  0: Metadata written succesfully
###  1: An error occourred. Metadata may have partially been written.
###  2: Interrupted by the user (answering a question). No data written.
###

## Functions
# substFile (fname) : void => string
# Substitute a file and print the result to STDOUT
function substFile ()
{
    fname=$1

    sed -e "s:\!\!SOURCE\!\!:$PWD:g" \
        -e "s:\!\!EXTENSIONS\!\!:$ext_list:g" \
        -e "s:\!\!PKGCONFIGINFILE\!\!$pkgconfig_infile:g" \
        $fname
}


## Main
meta_dir=meta
default_editor=vi
#TODO: uncomment the share_dir line and remove the other definition.
#share_dir=/usr/share/icecode/project
share_dir=share

if [[ -z $EDITOR ]]; then
    echo -n "Please type your editor's name [$default_editor]: "
    read EDITOR
    
    if [[ -z $EDITOR ]]; then EDITOR="$default_editor"; fi
fi

if [[ -d "$meta_dir" ]]; then
    echo -n -e "The $meta_dir metadata directory exists.\nContent may be overridden. Proceed? [y/N] "
    read ans

    if [[ "$ans" != "y" ]] && [[ "$ans" != "Y" ]]; then
        exit 2
    fi
else
    echo "Creating metadata directory..."
    mkdir "$meta_dir"
    if [[ $? != 0 ]]; then exit 1; fi
fi

cat > "$meta_dir/VERSION" <<EOF
program_name version_major version_minor version_phase
EOF

cat > "$meta_dir/DESCRIPTION" <<EOF
One-line description of the program/library.
EOF

cat > "$meta_dir/RELEASES" <<EOF
$(date +"%Y")
EOF

cat > "$meta_dir/AUTHOR" <<EOF
One line list of authors (should be comma separeted).
EOF

install -m 744 "$share_dir/common.sh" .
install -m 744 "$share_dir/update-licenses.sh" .
install -m 744 "$share_dir/update-files.sh" .
install -m 744 "$share_dur/help.sh" .
